<template>
  <div class="expenseList">
    <filterPanel filterName="SExpenseListFilter" filterTitle="费用查询" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="SExpenseListTable" :tableLoading="loading" tableTitle="费用列表" :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button type="primary" @click="tableClick('', 'add')">新增</zy-button>
    </tablePanel>
    <zyModel :show="showModel" :title="modelTitle" @submit="submit" @close="closeModel">
      <div class="personalChange">
        <zy-modal-info :model-info="modelInfo"></zy-modal-info>
      </div>
    </zyModel>
  </div>
</template>

<script>
	export default {
		name: "SExpenseList",
		props: {},
		data() {
			return {
				filterData: [
					{
						title: '费用类型',
						name: 'feetype',
						type: 'select',
						optionList: [
							{value: '1', label: "其他费用"},
							{value: '2', label: "工资"},
							{value: '3', label: "材料购置"},
							{value: '4', label: "员工福利"},
							{value: '5', label: "员工补税"},
							{value: '6', label: "房租"},
							{value: '7', label: "水电"},
						],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建人',
						name: 'name',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						name: 'time',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
				],
				form: {
					status: "",
					time: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '费用类型',
						width: 120,
						name: 'feetype',
						isShow: true,
						isSelected: true
					},
					{
						title: '费用金额',
						width: 120,
						name: 'fee',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建人',
						width: 120,
						name: 'name',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						width: 120,
						name: 'ctm',
						isShow: true,
						isSelected: true
					},
					{
						title: '备注',
						width: 120,
						name: 'feeinfo',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						width: 120,
						name: 'handler',
						fixed: 'right',
						btns: [
							{
								type: 'delete',
								name: '删除',
                btnType:'danger'
							},
							{
								type: 'edit',
								name: '修改'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				showModel: false,
				modelTitle: '',
				modelInfo: [
					{
						title: '费用类型',
						name: 'feetype',
						type: 'select',
						value: '',
						optionList: [
							{value: '1', label: "其他费用"},
							{value: '2', label: "工资"},
							{value: '3', label: "材料购置"},
							{value: '4', label: "员工福利"},
							{value: '5', label: "员工补税"},
							{value: '6', label: "房租"},
							{value: '7', label: "水电"},
						],
						titleClass: 'required'
					},
					{
						title: '费用金额',
						name: 'fee',
						type: 'input',
						value: '',
						titleClass: 'required'
					},
					{
						title: '备注',
						name: 'feeinfo',
						type: 'input',
						value: '',
						titleClass: 'required'
					},
				],
				fee_id: ''
			};
		},
		computed: {
			postData() {
				let data = {
					...this.form,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.time && this.form.time.length > 0) {
					data.start_time = this.form.time[0] / 1000;
					data.end_time = this.form.time[1] / 1000;
				}
				return data;
			},
		},
		watch: {},
		components: {},
		methods: {
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.$axios.post(this.$api.lstfeelog, this.postData).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						let feetypeList = ['', "其他费用", "工资", "材料购置", "员工福利", "员工补税", "房租", "水电"]
						this.tableData = data.list.filter(item => {
							item.feetype = feetypeList[item.feetype]
							item.delete = true;
							item.edit = true;
							return item;
						});
						this.total = data.pagination && data.pagination.total_count ? data.pagination.total_count : this.tableData.length;
					} else {
						this.total = 0;
						this.tableData = [];
					}
				}).catch(ret => {
					this.loading = false;
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				switch (type) {
					case 'add':
						this.modelTitle = '新增费用';
						this.fee_id = '';
						this.type = type;
						this.showModel = true;
						break;
					case 'edit':
						this.modelTitle = '修改费用';
						this.type = type;
						this.showModel = true;
						this.modelInfo = this.modelInfo.filter(item => {
								for (var key in data) {
									if (item.name === key) {
										item.value = data[item.name];
									}
								}
								return item
							}
						);
						this.fee_id = data.id;
						break;
					case 'delete':
						this.$confirm("是否删除该费用?", "提示", {
							confirmButtonText: "确定",
							cancelButtonText: "取消",
							type: "warning",
							customClass: 'message',
							closeOnClickModal: false
						}).then(() => {
							this.$axios.post(this.$api.rmfeelog, {
								id: data.id
							}).then(res => {
								let {code, msg, data} = res;
								this.loading = false;
								if (code == 200) {
									this.$message.success(msg);
									this.getData();
								} else {
									this.$message.error(msg);
								}
							}).catch(ret => {
								this.$message.error(ret.msg || '服务繁忙，请稍后再试');
							});
						}).catch(() => {
							this.$message("已取消删除");
						});
						break;
				}
			},
			submit() {
				let postData = {
					fee_id: this.fee_id
				};
				let tipText = '';
				this.modelInfo.forEach(item => {
					if (item.value) {
						postData[item.name] = item.value;
					} else {
						tipText = `${item.title}不能为空`
					}
				});
				if (tipText) {
					return this.$message.error(tipText);
				}

				this.$axios.post(this.$api.addfee, {
					...postData
				}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.closeModel();
						this.getData();
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			closeModel() {
				this.showModel = false;
			}
		},
		mounted() {
			this.getData();
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/sorting/expense/expenseList/index.less">

</style>
